#!/bin/bash

# RTX 4060Ti 16G显卡验证脚本
# 作者：AI助手
# 功能：自动化验证显卡真伪和性能

# 显示彩色输出
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # 无颜色

# 打印标题
echo -e "${GREEN}============================================${NC}"
echo -e "${GREEN}     RTX 4060Ti 16G 显卡验证工具         ${NC}"
echo -e "${GREEN}============================================${NC}"
echo ""

# 检查是否为root用户
if [ "$EUID" -ne 0 ]; then
  echo -e "${RED}请使用root权限运行此脚本：sudo $0${NC}"
  exit 1
fi

# 确认用户意愿
echo -e "${YELLOW}此脚本将安装必要的工具并执行显卡测试。${NC}"
echo -e "${YELLOW}测试过程可能导致系统负载增加并使显卡温度升高。${NC}"
read -p "确定要继续吗？(y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  echo -e "${RED}操作已取消${NC}"
  exit 1
fi

# 安装必要工具
echo -e "\n${GREEN}[步骤1] 安装必要工具${NC}"
echo "更新软件源..."
apt update

echo "安装基础工具包..."
apt install -y pciutils lm-sensors nvidia-driver-545 nvidia-cuda-toolkit mesa-utils hardinfo glmark2 stress-ng smartmontools hwinfo

echo "安装性能测试工具..."
apt install -y unigine-valley unigine-heaven glmark2 gputest

# 验证显卡信息
echo -e "\n${GREEN}[步骤2] 验证显卡基本信息${NC}"
echo -e "${YELLOW}PCI设备信息:${NC}"
lspci | grep -i nvidia
echo ""

echo -e "${YELLOW}详细显卡信息:${NC}"
lshw -C display | grep -i -E "product|vendor|bus info|description|clock|width|capabilities"
echo ""

echo -e "${YELLOW}显卡硬件信息:${NC}"
hwinfo --gfxcard | grep -i -E "Model|Vendor|Memory Size|Device|Clock"
echo ""

# 检查NVIDIA驱动
echo -e "\n${GREEN}[步骤3] 检查NVIDIA驱动${NC}"
if command -v nvidia-smi &> /dev/null; then
  echo -e "${YELLOW}NVIDIA驱动信息:${NC}"
  nvidia-smi
  
  # 提取显存信息
  MEM_INFO=$(nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits)
  echo -e "\n${YELLOW}显存大小: ${MEM_INFO} MB${NC}"
  
  # 检查是否为16GB
  if (( $(echo "$MEM_INFO > 15000" | bc -l) )); then
    echo -e "${GREEN}✓ 显存大小符合RTX 4060Ti 16G规格${NC}"
  else
    echo -e "${RED}✗ 警告：显存大小不符合RTX 4060Ti 16G规格${NC}"
  fi
  
  # 检查型号
  MODEL=$(nvidia-smi --query-gpu=name --format=csv,noheader)
  if [[ "$MODEL" == *"4060 Ti"* ]]; then
    echo -e "${GREEN}✓ 显卡型号符合RTX 4060Ti${NC}"
  else
    echo -e "${RED}✗ 警告：显卡型号不符合预期的RTX 4060Ti${NC}"
  fi
else
  echo -e "${RED}未检测到NVIDIA驱动，尝试安装...${NC}"
  ubuntu-drivers autoinstall
  echo -e "${YELLOW}驱动已安装，请重启系统后再次运行此脚本。${NC}"
  exit 1
fi

# 运行基础测试
echo -e "\n${GREEN}[步骤4] 基本性能测试${NC}"
echo -e "${YELLOW}是否运行基本性能测试？这将打开图形窗口。(y/n)${NC}"
read -p "" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
  echo "运行glxgears基本测试..."
  glxgears &
  GLXGEARS_PID=$!
  sleep 10
  kill $GLXGEARS_PID
  
  echo -e "\n运行glmark2标准测试..."
  glmark2 --fullscreen
fi

# 运行CUDA测试
echo -e "\n${GREEN}[步骤5] CUDA测试${NC}"
echo -e "${YELLOW}是否运行CUDA测试？(y/n)${NC}"
read -p "" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
  echo "安装CUDA示例..."
  apt install -y nvidia-cuda-samples
  
  # 检查deviceQuery
  if [ -d "/usr/local/cuda/samples/1_Utilities/deviceQuery" ]; then
    echo "运行deviceQuery..."
    cd /usr/local/cuda/samples/1_Utilities/deviceQuery
    make
    ./deviceQuery
  else
    echo -e "${RED}找不到CUDA示例，尝试从GitHub下载...${NC}"
    apt install -y git
    cd /tmp
    git clone https://github.com/NVIDIA/cuda-samples.git
    cd cuda-samples/Samples/1_Utilities/deviceQuery
    make
    ./deviceQuery
  fi
fi

# 运行压力测试
echo -e "\n${GREEN}[步骤6] 压力测试${NC}"
echo -e "${YELLOW}是否运行GPU压力测试？这将使显卡满载运行5分钟。(y/n)${NC}"
read -p "" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
  echo "启动监控..."
  gnome-terminal -- bash -c "watch -n 1 nvidia-smi" &
  MONITOR_PID=$!
  
  echo "开始压力测试(5分钟)..."
  stress-ng --gpu 1 --timeout 5m
  
  # 终止监控
  kill $MONITOR_PID
fi

# 温度和功耗测试
echo -e "\n${GREEN}[步骤7] 温度和功耗测试${NC}"
echo -e "${YELLOW}显卡当前温度和功耗:${NC}"
nvidia-smi --query-gpu=temperature.gpu,power.draw --format=csv

# VBIOS检查
echo -e "\n${GREEN}[步骤8] VBIOS检查${NC}"
echo -e "${YELLOW}显卡BIOS版本:${NC}"
nvidia-smi -q | grep "VBIOS"

# 序列号
echo -e "\n${GREEN}[步骤9] 显卡序列号${NC}"
echo -e "${YELLOW}尝试获取显卡序列号:${NC}"
dmidecode | grep -A3 "VGA"

# 生成测试报告
echo -e "\n${GREEN}[步骤10] 生成测试报告${NC}"
REPORT_FILE="gpu_test_report_$(date +%Y%m%d_%H%M%S).txt"
echo "测试报告生成中..."

{
  echo "RTX 4060Ti 16G显卡测试报告"
  echo "=========================="
  echo "测试时间: $(date)"
  echo ""
  echo "基本信息:"
  lspci | grep -i nvidia
  echo ""
  echo "NVIDIA驱动信息:"
  nvidia-smi
  echo ""
  echo "温度和功耗:"
  nvidia-smi --query-gpu=temperature.gpu,power.draw --format=csv
  echo ""
  echo "VBIOS版本:"
  nvidia-smi -q | grep "VBIOS"
} > "$REPORT_FILE"

echo -e "${GREEN}测试报告已保存至: $REPORT_FILE${NC}"

# 结论提示
echo -e "\n${GREEN}[测试完成]${NC}"
echo -e "${YELLOW}请根据以下标准评估显卡:${NC}"
echo "1. RTX 4060Ti正常应有16GB显存"
echo "2. 温度在满载下应保持在85°C以下"
echo "3. 性能测试应无明显卡顿或图像错误"
echo "4. 显卡信息应与官方规格一致"
echo ""
echo -e "${YELLOW}注意事项:${NC}"
echo "- 若显卡报告显示非4060Ti或显存非16GB，可能是假卡"
echo "- 若测试过程温度迅速升高超过90°C，可能散热系统受损"
echo "- 若压力测试时出现严重降频，可能是矿卡或已损坏的卡"
echo ""
echo -e "${GREEN}感谢使用RTX 4060Ti 16G显卡验证工具!${NC}" 